﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartMD.DataModel.Entity;
using SmartMD.Manager.Common;
using SmartMD.DataAccess.Service;
using Techstar.Dialog;
using Techstar.Progress;
using System.Data;
using SmartMD.DataAccess.Oracle;
using System.Collections.ObjectModel;
using SmartMD.Manager.Specimens;
using SmartMD.Manager.Utils;
using SmartMD.DataModel;
using SmartMD.Manager.Specimens.Store;

namespace SmartMD.Manager.Specimens
{
    /// <summary>
    /// Interaction logic for StoreSpecPanel.xaml
    /// </summary>
    public partial class StoreSpecPanel : UserControl
    {
        public bool isSaved = true;
        private int MAX_SPEC_NUM = 300;

        public StoreSpecPanel()
        {
            InitializeComponent();
            
            this.toolbar.saveHandler = this.SaveSpec;
            this.toolbar.newHandler = this.NewSpec;
            this.toolbar.deleteHandler = this.DeleteSpec;
            this.ucSpecStoreEdit.IsEnabled = false;
            this.ucSpecList.Owner = this;           
        }

        public void OnWindowActivated()
        {
            this.ucSpecStoreEdit.InitAllComboxValue();
        }
        private  NewWindow newDlg = null;
        private void NewSpec()
        {
            List<SpecEntity> specEntityList = DataServiceManager.SpecimensService.GetAllSpecEntities();
            if (specEntityList.Count >= MAX_SPEC_NUM)
            {
                TaskDialogEx.Warning("提示信息", "请联系多圈网医学研究中心，感谢您的试用！");
                return;
            }
            //验证用户输入
            if (!this.isSaved && !this.ucSpecStoreEdit.Verify())
                return;

          /*  newDlg = new NewWindow();            
            newDlg.Title = "新建标本";
            newDlg.label1.Content = "标本名称";
            newDlg.label2.Content = "描    述";
            newDlg.okHandler = NewWindow_OkClick;
            newDlg.ShowDialog();
           * */
            NewSpecDialog dlg = new NewSpecDialog(this);
            dlg.ShowDialog();
        }

      /*  private void NewWindow_OkClick(object sender, RoutedEventArgs e)
        {
            SpecInfo spec = new SpecInfo();
            spec.Id = System.Guid.NewGuid().ToString();
            spec.FollowUpDate = System.DateTime.Now;
            spec.Name = newDlg.txt1.Text;
            spec.State = DataModel.RecordState.Added;
            this.ucSpecStoreEdit.DataSource = spec;
            this.ucSpecList.AddItem(spec);
            newDlg.Close();
            this.ucSpecStoreEdit.IsEnabled = true;
            this.ucSpecStoreEdit.BtnSearchSite.IsEnabled = true;
            //标识未保存状态
            this.isSaved = false;
        }*/

        private void SaveSpec()
        {
            //验证用户输入
            if (!this.ucSpecStoreEdit.Verify())
                return;
            List<SpecEntity> specEntityList = new List<SpecEntity>();
            ObservableCollection<SpecInfo> tmpSpecInfos = this.ucSpecList.specInfoList;
            ObservableCollection<SpecInfo> specInfos = new ObservableCollection<SpecInfo>();
            for (int k = 0; k < tmpSpecInfos.Count; k++)
            {
                if (tmpSpecInfos[k].State == RecordState.Added)
                {
                    specInfos.Add(tmpSpecInfos[k]);
                }
            }
            for (int j = 0; j < specInfos.Count; j++)
            {
                ObservableCollection<SiteSelectEntity> siteselectEntities = specInfos[j].SiteEntityList;
                for (int i = 0; i < siteselectEntities.Count; i++)
                {
                    SpecEntity specEntity = new SpecEntity();
                    specEntity.Id = specInfos[j].SpecType + GuidUtil.NewGuid(); //System.Guid.NewGuid().ToString();
                    specEntity.SpecId = siteselectEntities[i].SpecId;
                    specEntity.SiteCode = siteselectEntities[i].SiteCode;
                    specEntity.CustomCode = siteselectEntities[i].CustomCode;
                    specEntity.EntityState = SpecState.NotUsed;
                    specEntity.StoredDate = System.DateTime.Now;

                    specEntityList.Add(specEntity);
                }
            }    
            bool isSaved = false;
            bool success = false;
            var progressThread = new ProgressThread(250);
            progressThread.Start("开始保存", "正在保存标本......");
            try
            {
                isSaved = DataServiceManager.SpecimensService.saveSpecimens(specInfos);
                success = DataServiceManager.SpecimensService.SaveSpecEntities(specEntityList);
                ModelMaintain.getInstance().SaveSpecEntities(specEntityList);

                for(int k=0;k<specEntityList.Count;k++) 
                {
                    DataServiceManager.ContainerService.UpdateContainerState(specEntityList[k].SiteCode, true);                
                }
            }
            finally
            {
                progressThread.End();
            }
            if (isSaved&&success)
            {
                for (int j = 0; j < this.ucSpecList.specInfoList.Count; j++)
                {
                    this.ucSpecList.specInfoList[j].State = DataModel.RecordState.Normal;
                }
                TaskDialogEx.Warning("提示", "保存成功！");
                //this.ucSpecList.specEntityList.Clear();
                this.ucSpecStoreEdit.Clear();
                this.isSaved = true;
            }
            else 
            {
                TaskDialogEx.Warning("保存失败", "请检查数据库连接状态！");
            }
            
        }

        private void DeleteSpec()
        {
            MessageBox.Show("delete");
        }

    }
}
